package com.goodving.guli.fragment;

import android.app.Dialog;
import android.app.Fragment;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.goodving.guli.R;
import com.goodving.guli.bt.DimPanelFragment;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.eventbus.EventPersonInfoUpdate;
import com.goodving.guli.bt.eventbus.EventUserInfoUpdate;
import com.goodving.guli.bt.model.PersonInfo;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.weight.UserInfo;
import com.goodving.guli.bt.weight.UserInfoManager;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.view.C3247b;
import com.goodving.guli.view.C3657a;

import de.greenrobot.event.EventBus;

/**
 * Created by john on 2015/12/24.
 */
public class NickNamePanelFragment extends DimPanelFragment { // dh
    private static final String f9955a;
    private static int f9956e;
    private UserInfo f9957b;
    private PersonInfo f9958c;
    private EditText f9959d;

    static {
        f9955a = NickNamePanelFragment.class.getSimpleName();
    }

    public static Fragment m14664a(int i) {
        GuliLog.tracePrintInfo(f9955a, "receive the uid" + i);
        f9956e = i;
        return new NickNamePanelFragment();
    }

    protected int inflateLayout() {
        return R.layout.fragment_person_info_setting_nick_name;
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (f9956e == -1) {
            this.f9958c = Keeper.readPersonInfo();
        } else {
            this.f9957b = UserInfoManager.getInstance().getInfoByUid(f9956e);
        }
    }

    public Dialog onCreateDialog(Bundle bundle) {
        return super.onCreateDialog(bundle);
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View onCreateView = super.onCreateView(layoutInflater, viewGroup, bundle);
        this.f9959d = (EditText) onCreateView.findViewById(R.id.info_nick_name);
        if (this.f9958c != null) {
            this.f9959d.setText(this.f9958c.nickname != null ? this.f9958c.nickname : C3657a.f18328f);
        } else {
            this.f9959d.setText(this.f9957b.name != null ? this.f9957b.name : C3657a.f18328f);
        }
        this.f9959d.setSelection(this.f9959d.length());
        return onCreateView;
    }

    protected void onLeftButtonClicked() {
        super.onLeftButtonClicked();
        dismiss();
    }

    protected void onRightButtonClicked() {
        String str;
        if (this.f9958c != null) {
            str = this.f9958c.nickname;
            if (TextUtils.isEmpty(this.f9959d.getText())) {
                this.f9958c.nickname = C3657a.f18328f;
            } else {
                this.f9958c.nickname = this.f9959d.getText().toString().trim();
            }
            if (!Utils.m8045b(this.f9958c.nickname)) {
                C3247b.m22928a(getActivity(), (int) R.string.content_too_short, 0).show();
                return;
            } else if (!Utils.m8072g(this.f9958c.nickname)) {
                C3247b.m22928a(getActivity(), (int) R.string.invalid_name, 0).show();
                return;
            } else if (this.f9958c.nickname.equals(str)) {
                dismiss();
                return;
            } else {
                this.f9958c.setNeedSyncServer(2);
                Keeper.keepPersonInfo(this.f9958c);
                EventBus.getDefault().post(new EventPersonInfoUpdate(str));
            }
        } else {
            str = this.f9957b.name;
            if (TextUtils.isEmpty(this.f9959d.getText())) {
                this.f9957b.name = C3657a.f18328f;
            } else {
                this.f9957b.name = this.f9959d.getText().toString().trim();
            }
            if (!Utils.m8045b(this.f9957b.name)) {
                C3247b.m22928a(getActivity(), (int) R.string.content_too_short, 0).show();
                return;
            } else if (!Utils.m8072g(this.f9957b.name)) {
                C3247b.m22928a(getActivity(), (int) R.string.invalid_name, 0).show();
                return;
            } else if (this.f9957b.equals(str)) {
                dismiss();
                return;
            } else {
                EventBus.getDefault().post(new EventUserInfoUpdate(this.f9957b));
            }
        }
        dismiss();
    }
}
